//  AddSomepersonVC.m
//  CXJZHP
//
//  Created by Mr.hu on 2017/7/29.
//  Copyright © 2017年 Mr.hu. All rights reserved.
//

#import "AddSomepersonVC.h"
#import "RegisterSucces.h"
#import "SkillModel.h"
#define START NSDate *startTime = [NSDate date]
#define END NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])

#define  WidthScreen  [UIScreen mainScreen].bounds.size.width
#define  HeightScreen  [UIScreen mainScreen].bounds.size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface AddSomepersonVC ()
{
    NSIndexPath *_currentLongPressIndex;
    SkillModel *_model;
}

/** 注释 **/
@property (strong, nonatomic) AddSomepersonVC * addsomevc;
/** 注释 **/
@property (strong, nonatomic) UITableView * tableView;
@property (nonatomic, copy) NSDictionary *contactPeopleDict;
/** 注释 **/
@property (strong, nonatomic) NSArray * dataArr; //存储存储模型的数组

@property (nonatomic, copy) NSArray *keys;
/** 注释 **/
@property (assign, nonatomic) CGFloat topheadHeight;
/** 技能ID **/
@property (strong, nonatomic) NSMutableArray * namearr;
@property (nonatomic,strong)  NSMutableArray * Skill_name;

@property (nonatomic, strong) NSIndexPath * indexPath;
/** 注释 **/

@property (nonatomic, assign) BOOL isSearchState;

@property (nonatomic, assign) BOOL isNeedRefresh;//是否需要刷新

@property(nonatomic,copy)AddSomePerson myBlock;
/** 注释 **/
@property (strong, nonatomic) UILabel * bottomlab;
/** 注释 **/
@property (assign, nonatomic) BOOL Viewstateboole;
/** 注释 **/
//@property (strong, nonatomic) UIView * firstView;

@end

@implementation AddSomepersonVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

-(instancetype)initWithBlock:(AddSomePerson)block
{
    if (self = [super init]) {
        self.myBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.Viewstateboole = NO;
    
    self.namearr = [NSMutableArray array];
    self.Skill_name = [NSMutableArray array];
    [self.namearr removeAllObjects];
    [self.Skill_name removeAllObjects];
    self.numberStr = [NSMutableArray array];
    self.dataArr = [NSMutableArray array];
    
    _topheadHeight = 66;
    
    [XJHttpClass TeachSkillListFinishblock:^(id response, NSString *errStr) {
        if (errStr) {
            [XJToolClass tipAnimationWith:errStr];
        }else{
            [SkillModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data" : @"SkillInfoModel"
                         };
            }];
            _model = [SkillModel mj_objectWithKeyValues:response];
            self.dataArr = response[@"data"];
            [self.tableView reloadData];
        }
    }];
    
    [self titleNavGation];//导航栏
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.myfriendtext resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.myfriendtext endEditing:YES];
    return YES;
}

- (void)titleNavGation{
    
    self.title = @"选择技能";
    //控制器中这么设置也可以
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16.f],NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    _myfriendtext.delegate = self;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    _indexPath = 0;
    
    _leftbutton = [[UIButton alloc]initWithFrame:CGRectMake(-10, 10, 60, 44)];
    _leftbutton.titleLabel.textAlignment = NSTextAlignmentRight;
    
    if (self.namearr.count>0) {
        [_leftbutton setTitle:[NSString stringWithFormat:@"确定(%lu)",(unsigned long)self.namearr.count] forState:UIControlStateNormal];
    }else{
       [_leftbutton setTitle:[NSString stringWithFormat:@"确定(0)"] forState:UIControlStateNormal];
    }
    
    [_leftbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _leftbutton.titleLabel.font = [UIFont systemFontOfSize:14];
    _leftbutton.titleLabel.textAlignment = NSTextAlignmentLeft;
    [_leftbutton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    UIBarButtonItem * leftbtns = [[UIBarButtonItem alloc]initWithCustomView:_leftbutton];
    [_leftbutton addTarget:self action:@selector(leftbuttonOnclicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = leftbtns;
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 60;
 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 40, 40);
    [btn setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(selectImageMethback) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
}

- (void)selectImageMethback{
    
    [self.navigationController popViewControllerAnimated:false];
};

- (void)leftbuttonOnclicked{
    
    NSString * SkillTech = [[NSString alloc]init];
    NSString * SkillTitle = [[NSString alloc]init];
    //技能ID
    for (NSString * choiceskill in self.namearr) {
        SkillTech = [NSString stringWithFormat:@"%@,%@",choiceskill,SkillTech];
    }
    NSString *skilltech_corrent = [SkillTech substringToIndex:[SkillTech length] -1];
    //技能名字
    for (NSString * choiceskill in self.Skill_name) {
        SkillTitle = [NSString stringWithFormat:@"%@,%@",choiceskill,SkillTitle];
    }
    NSString *skilltech_title = [SkillTitle substringToIndex:[SkillTitle length]-1];
    //token没有用到
    [XJHttpClass FixSkillToken:[XJToolClass getToken] skill:skilltech_corrent andFinishblock:^(id response, NSString *errStr) {
        if (errStr) {
            [XJToolClass tipAnimationWith:errStr];
        }else{
            
            
            self.myBlock(skilltech_corrent, skilltech_title);
            [self.navigationController popViewControllerAnimated:false];
            [XJToolClass tipAnimationWith:@"选择成功！"];
        }
    }];
}

- (void)backViewOnclicked{
    
    [self.navigationController popViewControllerAnimated:false];
}

#pragma mark - TableViewDatasouce/TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];
    
    _cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (_cell == nil){
        _cell = [[HPAddSomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSArray *currentArr = [self.contactStr  componentsSeparatedByString:@","];
    
    
    if ([currentArr containsObject:self.dataArr[indexPath.row][@"name"]]) {
         [_cell updateCellWithState: !_cell.isSelect];
    }
    
    _cell.namelab.text = self.dataArr[indexPath.row][@"name"];
    
    return _cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    
        if (self.namearr.count <= 10) {
        
        HPAddSomeCell * cell = [tableView cellForRowAtIndexPath:indexPath];
           
        [cell updateCellWithState: !cell.isSelect];
        
        if (cell.isSelect) {
            
            if (self.namearr.count < 10) {
            [self.namearr addObject:[NSString stringWithFormat:@"%@",self.dataArr[indexPath.row][@"cate_id"]]];
            [self.Skill_name addObject:self.dataArr[indexPath.row][@"name"]];
                
            [_leftbutton setTitle:[NSString stringWithFormat:@"确定(%lu)",(unsigned long)self.namearr.count] forState:UIControlStateNormal];
            }
            
        }else{
            for(UIView *View in self.view.subviews )
            {
                for (int i = 0; i < self.namearr.count ; i++) {
                    if(View.tag == 100 + i || View.tag == 1000 + i)
                    {
                        [View removeFromSuperview];
                    }
                }
            }
            
            [self.namearr removeObject:self.dataArr[indexPath.row][@"cate_id"]];
            [self.Skill_name removeObject:self.dataArr[indexPath.row][@"name"]];
            [_leftbutton setTitle:[NSString stringWithFormat:@"确定(%lu)",(unsigned long)self.namearr.count] forState:UIControlStateNormal];
            }
        }else{
            
        }
    
    
        [self.namebtn removeFromSuperview];
        [self.bottomlab removeFromSuperview];
}


- (void)deletenameonclicked:(UIButton *)sender{
    
        
}

@end
